/**
 * 自定义选择组件
 *
 * searchType:   multipleChoice(多选)    singleChoice(单选)  secondaryLinkage 二级联动
 *  singleSort(单行排序)     more： 综合赛选自定义
 *
 * 入参：
 * searchData{
 *    title:'',
 *    searchKey:'',
 *    searchType:'',
 *    data:[
 *        {
 *        title:''
 *        id:''
 *        }
 *    ]
 * }
 */
Component({
  properties: {
    searchData: {
      type: Array,
      value: [],
    },
  },
  data: {
    i: 0,
    orgTitles: [],
  },
  methods: {
    setOrg() {
      let i = this.data.i;
      if (i == 0) {
        let orgTitles = [].concat(this.data.searchData.map((it) => it.title));
        i = i + 1;
        this.setData({
          orgTitles,
          i,
        });
      }
    },
    selectIndex(e) {
      this.setOrg();
      let { index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      for (let i = 0; i < searchData.length; i++) {
        if (i == index) {
          searchData[i].selected = !searchData[i].selected;
        } else {
          searchData[i].selected = false;
        }
      }
      this.setData({
        searchData,
      });
    },
    closeView() {
      let searchData = this.data.searchData;
      for (let i = 0; i < searchData.length; i++) {
        searchData[i].selected = false;
      }
      this.setData({
        searchData,
      });
      this.getSelectData();
    },
    getSelectData() {
      let searchData = this.data.searchData;
      let orgTitles = this.data.orgTitles;
      let selectDatas = {};
      for (let i = 0; i < searchData.length; i++) {
        if (searchData[i].searchType == "more") {
          let moreData = searchData[i].data;
          for (let i = 0; i < moreData.length; i++) {
            let searchKey = moreData[i].searchKey;
            let searchValue = moreData[i].data
              .filter((it) => it.selected)
              .map((it) => it.id);

            if (moreData[i].searchType == "multipleChoice") {
              if (searchValue.length > 0) {
                selectDatas[searchKey] = searchValue;
              }
            } else {
              if (searchValue.length > 0) {
                selectDatas[searchKey] = searchValue[0];
              }
            }
          }
        } else {
          let searchKey = searchData[i].searchKey;
          let searchValue = searchData[i].data
            .filter((it) => it.selected)
            .map((it) => it.id);
          if (searchData[i].searchType == "multipleChoice") {
            if (searchValue.length > 0) {
              selectDatas[searchKey] = searchValue;
            }
          } else {
            if (searchValue.length > 0) {
              selectDatas[searchKey] = searchValue[0];
            }
          }
          if (searchData[i].rightData) {
            let rightSearchDatas = searchData[i].rightData
              .filter((it) => it.selected)
              .map((it) => it.id);
            if (rightSearchDatas.length > 0) {
              let key = searchKey + "_right";
              selectDatas[key] = rightSearchDatas[0];
            }
          }
        }

        let org_title = orgTitles[i];

        if (
          searchData[i].searchType == "singleChoice" ||
          searchData[i].searchType == "singleSort" ||
          searchData[i].searchType == "multipleChoice"
        ) {
          let title = "";
          try {
            title = searchData[i].data
              .filter((it) => it.selected)
              .map((it) => it.title);
            title = title.join("、");
          } catch (error) {}

          searchData[i].title = title || org_title;
        } else if (searchData[i].searchType == "secondaryLinkage") {
          let title = "";
          try {
            let selectValue = searchData[i].data.filter((it) => it.selected)[0];
            if (selectValue.id) {
              title = selectValue.title;
            }
          } catch (error) {}
          try {
            let selectValue = searchData[i].rightData.filter(
              (it) => it.selected
            )[0];
            if (selectValue.id) {
              title = selectValue.title;
            }
          } catch (error) {}
          searchData[i].title = title || org_title;
        }
      }

      this.setData({
        searchData,
      });
     
      this.triggerEvent("getSearchData", selectDatas); // 将num通过参数的形式传递给父组件
    },

    multipleChoice(e) {
      let { item_index, data_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      for (let i = 0; i < searchData[item_index].data.length; i++) {
        if (i == data_index) {
          searchData[item_index].data[i].selected =
            !searchData[item_index].data[i].selected;
        }
      }
      this.setData({
        searchData,
      });
    },
    singleChoice(e) {
      let { item_index, data_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      for (let i = 0; i < searchData[item_index].data.length; i++) {
        if (i == data_index) {
          searchData[item_index].data[i].selected =
            !searchData[item_index].data[i].selected;
        } else {
          searchData[item_index].data[i].selected = false;
        }
      }
      this.closeView();
      this.setData({
        searchData,
      });
    },
    resetSearch(e) {
      let { item_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      for (let i = 0; i < searchData[item_index].data.length; i++) {
        searchData[item_index].data[i].selected = false;
      }
      this.setData({
        searchData,
      });
    },

    more_multipleChoice(e) {
      let { item_index, data_index, item_data_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      let model = searchData[item_index].data[item_data_index];
      for (let i = 0; i < model.data.length; i++) {
        if (i == data_index) {
          model.data[i].selected = !model.data[i].selected;
        }
      }
      searchData[item_index].data[item_data_index] = model;
      this.setData({
        searchData,
      });
    },
    more_singleChoice(e) {
      let { item_index, data_index, item_data_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      let model = searchData[item_index].data[item_data_index];
      for (let i = 0; i < model.data.length; i++) {
        if (i == data_index) {
          model.data[i].selected = !model.data[i].selected;
        } else {
          model.data[i].selected = false;
        }
      }
      searchData[item_index].data[item_data_index] = model;
      this.setData({
        searchData,
      });
    },
    more_resetSearch(e) {
      let { item_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      let moreData = searchData[item_index].data;
      for (let i = 0; i < moreData.length; i++) {
        for (let j = 0; j < moreData[i].data.length; j++) {
          moreData[i].data[j].selected = false;
        }
      }
      searchData[item_index].data = moreData;
      this.setData({
        searchData,
      });
    },
    secondaryLinkageLeft(e) {
      let { item_index, data_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      for (let i = 0; i < searchData[item_index].data.length; i++) {
        if (i == data_index) {
          searchData[item_index].data[i].selected =
            !searchData[item_index].data[i].selected;
        } else {
          searchData[item_index].data[i].selected = false;
        }
      }

      if (searchData[item_index].data[data_index].selected) {
        let rightData = [
          {
            title: "不限",
          },
        ].concat(searchData[item_index].data[data_index].childModel);
        searchData[item_index].rightData = rightData;
      } else {
        searchData[item_index].rightData = [];
      }

      this.setData({
        searchData,
      });
    },
    secondaryLinkageRight(e) {
      let { item_index, data_index } = e.currentTarget.dataset;
      let searchData = this.data.searchData;
      for (let i = 0; i < searchData[item_index].rightData.length; i++) {
        if (i == data_index) {
          searchData[item_index].rightData[i].selected =
            !searchData[item_index].rightData[i].selected;
        } else {
          searchData[item_index].rightData[i].selected = false;
        }
      }
      if (searchData[item_index].rightData[data_index].selected) {
        this.closeView();
      }
      this.setData({
        searchData,
      });
    },
  },
});
